home *** CD-ROM | disk | FTP | other *** search
/ Computer Select (Limited Edition) / Computer Select.iso / dobbs / v17n05 / wincomm.exe / COMM.C next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  4.1 KB  |  152 lines

  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    COMM.C        - Written by Mike Sax for Dr. Dobb's Journal
  4. //
  5. //    This file implements a few higher-level communicatons functions under
  6. //    Microsoft Windows.
  7. //
  8. //    This file contains eight public functions:
  9. //
  10. //    int OpenComPort(int nPort);
  11. //    BOOL CloseComPort(int nPortID);
  12. //    BOOL SetComPortParameters(int nPortID, int nSpeed, char chParity,
  13. //            int nDataBits, int nStopBits, BOOL bXOnXOff, BOOL bHardware);
  14. //    int CharsWaitingToBeRead(int nPortID);
  15. //    int ComReadChar(int nPortID);
  16. //    int ComReadChars(int nPortID, char *pchBuffer, int cbBuffer);
  17. //    BOOL ComWriteChar(int nPortID, int nChar);
  18. //
  19. ////////////////////////////////////////////////////////////////////////////
  20.  
  21. #define USECOMM 1                // for 3.1 windows.h
  22. #include <windows.h>
  23. #include "comm.h"
  24.  
  25. // Opens the com. port (1 = COM1) and returns its ID value.
  26. // If an error occurs, the return value is negative
  27. int OpenComPort(int nPort)
  28.     {
  29.     char szPort[10];
  30.  
  31.     wsprintf(szPort, "COM%d", nPort);
  32.     // Open the port with a 4K input queue and a 2K output queue
  33.     return OpenComm(szPort, 4096, 2048);
  34.     }
  35.  
  36. // Closes the comm. port specified by nPortID
  37. // returns TRUE if success, FALSE if failure
  38. BOOL CloseComPort(int nPortID)
  39.     {
  40.     if (nPortID < 0)
  41.         return FALSE;
  42.     FlushComm(nPortID,0);       // Flush transmit queue
  43.     FlushComm(nPortID,1);       // Flush receive queue
  44.     return !CloseComm(nPortID);
  45.     }
  46.  
  47. // Sets the communications parameters of the port specified by nPortID.
  48. // returns TRUE if success, FALSE if failure
  49. BOOL SetComPortParameters(int nPortID, int nSpeed, char chParity, int nDataBits,
  50.                           int nStopBits, BOOL bXOnXOff, BOOL bHardware)
  51.     {
  52.     DCB dcb;
  53.  
  54.     if (nPortID < 0)
  55.         return FALSE;
  56.     dcb.Id = nPortID;
  57.     dcb.BaudRate = nSpeed;
  58.     dcb.ByteSize = (BYTE)nDataBits;
  59.     // Convert chParity to uppercase:
  60.     AnsiUpperBuff(&chParity, 1);
  61.     dcb.Parity =  (chParity == 'N') ? NOPARITY :
  62.                   (chParity == 'O') ? ODDPARITY :
  63.                   (chParity == 'E') ? EVENPARITY :
  64.                   (chParity == 'M') ? MARKPARITY : SPACEPARITY;
  65.     dcb.StopBits = (BYTE)((nStopBits == 1) ? ONESTOPBIT :
  66.                           (nStopBits == 2) ? TWOSTOPBITS : ONE5STOPBITS);
  67.     dcb.RlsTimeout= 0;
  68.     dcb.CtsTimeout = bHardware ? 30 : 0;
  69.     dcb.DsrTimeout = 0;
  70.     dcb.fBinary = TRUE;
  71.     dcb.fRtsDisable = FALSE;
  72.     dcb.fParity = FALSE;
  73.     dcb.fOutxCtsFlow = (BYTE)bHardware;
  74.     dcb.fOutxDsrFlow = FALSE;
  75.     dcb.fDummy = 0;
  76.     dcb.fDtrDisable = FALSE;
  77.     dcb.fOutX = (BYTE)bXOnXOff;
  78.     dcb.fInX = (BYTE)bXOnXOff;
  79.     dcb.fPeChar = FALSE;
  80.     dcb.fNull = FALSE;
  81.     dcb.fChEvt = FALSE;
  82.     dcb.fDtrflow = (BYTE)FALSE;
  83.     dcb.fRtsflow = (BYTE)bHardware;
  84.     dcb.fDummy2 = 0;
  85.     dcb.XonChar = 17;
  86.     dcb.XoffChar = 19;
  87.     dcb.XonLim = 4096 / 4;              // Receive buffer size / 4
  88.     dcb.XoffLim = dcb.XonLim;
  89.     dcb.EofChar = 26;
  90.     dcb.EvtChar = 0;
  91.     dcb.TxDelay = 0;
  92.     return !SetCommState(&dcb);
  93.     }
  94.  
  95. // Returns the number of characters waiting in the input queue
  96. int CharsWaitingToBeRead(int nPortID)
  97.     {
  98.     COMSTAT ComStat;
  99.  
  100.     if (nPortID < 0)
  101.         return 0;
  102.     GetCommError(nPortID, &ComStat);
  103.     return ComStat.cbInQue;
  104.     }
  105.  
  106. // Read a character from the port specified by nPortID
  107. // returns -1 if no character available
  108. int ComReadChar(int nPortID)
  109.     {
  110.     int iResult = 0;
  111.  
  112.     if (nPortID < 0)
  113.         return -1;
  114.     if (ReadComm(nPortID, (LPSTR)&iResult, 1) != 1)
  115.         {
  116.         iResult = -1;
  117.         GetCommError(nPortID, NULL);
  118.         }
  119.     return iResult;
  120.     }
  121.  
  122. // Read a character from the port specified by nPortID
  123. // returns the number of characters read or -1 if an error occurs.
  124. int ComReadChars(int nPortID, char *pchBuffer, int cbBuffer)
  125.     {
  126.     int iResult = 0;
  127.  
  128.     if (nPortID < 0)
  129.         return -1;
  130.     iResult = ReadComm(nPortID, pchBuffer, cbBuffer);
  131.     if (iResult < 0)
  132.         {
  133.         iResult = -1;
  134.         GetCommError(nPortID, NULL);
  135.         }
  136.     return iResult;
  137.     }
  138.  
  139. // Write a character to the port specified by nPortID
  140. // returns TRUE if success, FALSE if failure
  141. BOOL ComWriteChar(int nPortID, int nChar)
  142.     {
  143.     if (nPortID < 0)
  144.         return FALSE;
  145.     if (1 != WriteComm(nPortID, (LPSTR)&nChar, 1))
  146.         {
  147.         GetCommError(nPortID, NULL);
  148.         return FALSE;
  149.         }
  150.     return TRUE;
  151.     }
  152.